package com.daz.student.leaveMessage.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.daz.common.dwr.teacherPush;
import com.daz.student.leaveMessage.pojo.Message;
import com.daz.student.leaveMessage.server.impl.LeaveMessageSV;
import com.daz.teacher.leaveMessage.server.ILeaveMessageService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class MessageAction extends ActionSupport{
	
	private String msg;
	
	private Boolean success;
	
	private String messageId;

    private Date createTime;

    private String teacherId;

    private String studentId;

    private String lookStatus;

    private String sentStatus;

    private String text;
    
    private Boolean flag;

    @Autowired
	private LeaveMessageSV leaveMessageSV;
    
    private List<Message> list;
    
    private int size;
    
    
    public String insert(){
    	Message value = new Message();
    	if(studentId == null){
    		studentId = (String)ActionContext.getContext().getSession().get("studentId");
    	}
    	if(teacherId == null || studentId == null || text == null){
    		this.setSuccess(false);
    		this.setMsg("系统异常！");
    		return SUCCESS;
    	}
    	value.setCreateTime(new Date());
    	
    	value.setStudentId(studentId);
    	value.setTeacherId(teacherId);
    	value.setText(text);
    	value.setSentStatus("2");
    	value.setLookStatus("0");
    	try {Map<String, Object> session = ActionContext.getContext().getSession();
			leaveMessageSV.insert(value);
			teacherPush Push = new teacherPush();
			Push.sendMessageAuto(teacherId, text ,(String)session.get("userId"),(String)session.get("userName"));
			this.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.setSuccess(false);
    		this.setMsg("系统异常！");
		}
    	
    	return SUCCESS;
    }
    
    public String update(){
    	Message value = new Message();
    	
    	if(studentId == null){
    		studentId = (String)ActionContext.getContext().getSession().get("studentId");
    	}
    	value.setMessageId(messageId);
    	value.setLookStatus("1");
    
    	try {
			leaveMessageSV.updateByPrimaryKeySelective(value);
			this.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.setSuccess(false);
    		this.setMsg("系统异常！");
		}
    	return SUCCESS;
    }
    
    //查看历史
    public String qryBystudentId(){
    	Map<String, Object> map = new HashMap<String, Object>();
    	if(studentId == null){
    		studentId = (String)ActionContext.getContext().getSession().get("studentId");
    	}
    	map.put("studentId", studentId);
    	//map.put("lookStatus",1);
    	try {
			list  = leaveMessageSV.selectByParameters(map);
			this.setMsg(buildStr(list));
			this.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.setSuccess(false);
    		this.setMsg("系统异常！");
		}
    	return SUCCESS;
    }
    //查看未读
    public String qryNotLookBystudentId(){
    	Map<String, Object> map = new HashMap<String, Object>();
    	if(studentId == null){
    		studentId = (String)ActionContext.getContext().getSession().get("studentId");
    	}
    	map.put("studentId", studentId);
    	map.put("lookStatus",0);
    	map.put("sentStatus", 1);
    	try {
			list  = leaveMessageSV.selectByParameters(map);
			this.setSize(list.size());
			this.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.setSuccess(false);
    		this.setMsg("系统异常！");
		}
    	return SUCCESS;
    }
    
    public String showNewMessageStr(){
    	qryNotLookBystudentId();
    	StringBuffer str = new StringBuffer();
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
		int start=0;
		for(int i=start;i<list.size();i++){		
			Message value = new Message();
			value.setMessageId(list.get(i).getMessageId());
	    	value.setLookStatus("1");
	    	try {
				leaveMessageSV.updateByPrimaryKeySelective(value);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String dentity = null;
			Date date = list.get(i).getCreateTime();
			String dateString = format.format(date); 
			dentity="<span style='color:red'> ["+"导师"+"] </span>"+dateString;
			str.append("<div style='margin:10px 5px 10px 5px;'><img src='Image/im32x32.gif' width='20px'/>"+dentity+"<br>"+"<div style='background:#CDD7FF;border-radius: 5px;padding: 10px 10px;width: auto;'>"+list.get(i).getText()+"</div> </div>");
		}
    	this.msg = str.toString();
    	return SUCCESS;
    }
    
    public String buildStr(List<Message> items){
    	StringBuffer str = new StringBuffer();
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
		int start=0;
		if(items.size()>50) {
			start = items.size()-50;
		}
		for(int i=start;i<items.size();i++){		
			String dentity = null;
			Date date = items.get(i).getCreateTime();
			String dateString = format.format(date); 
			if("2".equals(items.get(i).getSentStatus())){
				dentity = " <span style='color:red'>[我] </span>"+dateString;
				str.append("<div style='margin:10px 5px 10px 5px;text-align:right;'><img src='Image/im32x32.gif' width='20px'/>"+dentity+"<div style='background:#FEEC98;border-radius: 5px;padding: 10px 10px;margin-right:5px;width: auto;'>"+items.get(i).getText()+"</div> </div>");
				//str.append("<div style='text-align:right;'><img src='Image/im32x32.gif' width='20px'/>"+dentity+"</div>");
				//str.append("<div style='background-color:#cfcfcf;width: 450px;'><div style='background-color:#CDD7FF;width: auto;float: right;border-radius: 5px;padding: 10px 10px;'>"+items.get(i).getText()+"</div></div>");
			}
			else{
				dentity="<span style='color:red'> ["+"导师"+"] </span>"+dateString;
				str.append("<div style='margin:10px 5px 10px 5px;'><img src='Image/im32x32.gif' width='20px'/>"+dentity+"<br>"+"<div style='background:#CDD7FF;border-radius: 5px;padding: 10px 10px;width: auto;'>"+items.get(i).getText()+"</div> </div>");
			}
			
		}
    	return str.toString();
    }
    
    
	public List<Message> getList() {
		return list;
	}

	public void setList(List<Message> list) {
		this.list = list;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public String getMessageId() {
		return messageId;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}

	public String getStudentId() {
		return studentId;
	}

	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}

	public String getLookStatus() {
		return lookStatus;
	}

	public void setLookStatus(String lookStatus) {
		this.lookStatus = lookStatus;
	}

	public String getSentStatus() {
		return sentStatus;
	}

	public void setSentStatus(String sentStatus) {
		this.sentStatus = sentStatus;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public Boolean getFlag() {
		return flag;
	}

	public void setFlag(Boolean flag) {
		this.flag = flag;
	}
    

}
